Ext.namespace('Ext.socialType');

Ext.socialType.validStore = [['1', '有效'], ['0', '无效']];
Ext.socialType.precisionStore = [['0', '整数'], ['1', '1位小数'], ['2', '2位小数'], ['3', '3位小数'], ['4', '4位小数']];
Ext.socialType.payAtMonthStore = [['0', '本月'], ['1', '上月']];
Ext.socialType.isForTaxStore = [['1', '需要缴税'], ['0', '不需要缴税']];
Ext.socialType.ISARREARSStore = [['1', '需要补缴'], ['0', '不需要补缴']];
Ext.socialType.payFrequencyStore = [['0', '月缴'], ['1', '年缴（不足一年按年缴）'],
		['2', '年缴（不足一年按月缴）']];
Ext.socialType.caculateTypeStore = [['1', '截位'], ['2', '四舍五入'],
		['3', '先四舍五入再向上进位'], ['4', '向上进位']];
var cityCombox;
var deletedJson = [];
var changedFlag = false;
function checkDate(strDate,rowIndex,colIndex){
   var reg=/^(\d{4})(\d{2})$/;
   if(!reg.test(strDate)){
   		  Ext.MessageBox.alert('Error','日期格式不正确!\n正确格式为:yyyyMM！',function(){
				Ext.getCmp('socialMasterInfoGrid').startEditing(rowIndex,colIndex);
		  });
          return false;
      }
  var year=strDate.substring(4,0);
  var month=strDate.substring(4);
  if(!checkMonth(month,rowIndex,colIndex)){return false;}
  return true;
}
function checkMonth(month,rowIndex,colIndex){
   if(isNaN(parseInt(month,10)))
   {
    	Ext.MessageBox.alert('Error','月份输入有误,请重新输入！',function(){
			Ext.getCmp('socialMasterInfoGrid').startEditing(rowIndex,colIndex);
		});
    	return false;
   }else if(parseInt(month,10)<1 || parseInt(month,10) >12)
      { 
        Ext.MessageBox.alert('Error','月份应该在1-12之间！',function(){
			Ext.getCmp('socialMasterInfoGrid').startEditing(rowIndex,colIndex);
		});
        return false;
      }
      else return true;
}
function addPbSocialTypeMasterInfo() {

	// 城市的下拉框
	cityCombox = new Ext.form.ComboBox({
		fieldLabel : '<image src="'+basePath+'images/must.gif"/>城市',
		name : 'pbSocialMaster.city_id',
		hiddenName : 'pbSocialMaster.city_id',
		id:'city_id',
		store : getCityStore(),
	    displayField : 'CITY_NAME',
		valueField : 'CITY_ID',
		typeAhead : true,
		mode : 'local',
		triggerAction : 'all',
		selectOnFocus : true,
		anchor : '90%',
		forceSelection: true,
		allowBlank:false,
		emptyText : '请选择...',
		blankText:'请输入城市!',
		width : 150/*,
        listeners:{
		    change:function(combo,record,index){
		    	alert("fda")
		    }
		}*/
	});
	// 省份的下拉框
	var proviceCombox = new Ext.form.ComboBox({
		fieldLabel : '<image src="'+basePath+'images/must.gif"/>省份',
		name : 'pbSocialMaster.province_id',
		hiddenName : 'pbSocialMaster.province_id',
		id:'province_id',
		store : getProvinceStore(),
		displayField : 'province_name',
		valueField : 'province_id',
		typeAhead : true,
		mode : 'local',
		triggerAction : 'all',
		selectOnFocus : true,
		anchor : '90%',
		forceSelection: true,
		allowBlank:false,
		emptyText : '请选择...',
		blankText:'请输入省份!',
		width : 150,
       listeners:{
		    select:function(combo,record,index){
		    	cityStore=cityCombox.getStore();
				cityStore.proxy = new Ext.data.HttpProxy({
					url : 'City!queryCityByProviceId.action?province_id='
							+ proviceCombox.getValue()
				});
			
				cityStore.load();
		    	cityCombox.reset();
		    }
		}
	});
	getProvinceStore().load();
	// 支付频率的下拉框
	var payFrequencyCombox = new Ext.form.ComboBox({
				fieldLabel : '支付频率',
				hiddenName:'pbSocialMaster.pay_frequency',
				name : 'pbSocialMaster.pay_frequency',
				id:'pay_frequency',
				store : new Ext.data.SimpleStore({
							fields : ['abbr', 'employStatus'],
							data : Ext.socialType.payFrequencyStore
						}),
				displayField : 'employStatus',
				typeAhead : true,
				value:'0',
				valueField : 'abbr',
				mode : 'local',
				triggerAction : 'all',
				selectOnFocus : true,
				emptyText : '请选择...',
				readOnly : true,
				anchor : '90%',
				emptyText : '请选择...',
				width : 150
			});

	// 所属类别的下拉框
	var socialTypeCombox = new Ext.form.ComboBox({
		fieldLabel : '<image src="'+basePath+'images/must.gif"/>所属类别',
		hiddenName:'pbSocialMaster.social_typeid',
		name : 'pbSocialMaster.social_typeid',
		id:'social_typeid',
		store : getSocialType(),
		displayField : 'social_typename',
		typeAhead : true,
		valueField : 'social_typeid',
		mode : 'local',
		triggerAction : 'all',
		selectOnFocus : true,
		anchor : '90%',
 		forceSelection: true,
		allowBlank:false,
		emptyText : '请选择...',
		blankText:'请输入所属类别!',
		width : 150
	});

	// 是否有效下拉框
	var isValidCombox = new Ext.form.ComboBox({
				fieldLabel : '是否有效',
				hiddenName:'pbSocialMaster.is_valid',
				name : 'pbSocialMaster.is_valid',
				id : 'is_valid',
				store : new Ext.data.SimpleStore({
							fields : ['key', 'value'],
							data : Ext.socialType.validStore
						}),
				displayField : 'value',
				typeAhead : true,
				valueField : 'key',
				value : '1',
				mode : 'local',
				triggerAction : 'all',
				selectOnFocus : true,
				emptyText : '请选择...',
				readOnly : true,
				anchor : '90%'
			});

	// 社保缴纳月份下拉框
	var payAtMonthCombox = new Ext.form.ComboBox({
				fieldLabel : '社保缴纳月份',
				hiddenName:'pbSocialMaster.pay_atmonth',
				name : 'pbSocialMaster.pay_atmonth',
				id : 'pay_atmonth',
				store : new Ext.data.SimpleStore({
							fields : ['key', 'value'],
							data : Ext.socialType.payAtMonthStore
						}),
				displayField : 'value',
				typeAhead : true,
				valueField : 'key',
				mode : 'local',
				value:'1',
				triggerAction : 'all',
				selectOnFocus : true,
				emptyText : '请选择...',
				readOnly : true,
				anchor : '90%'
			});

	// 企业金额精确值下拉框
	var precisionCombox = new Ext.form.ComboBox({
				fieldLabel : '企业金额精确值',
				hiddenName:'pbSocialMaster.precision',
				name : 'pbSocialMaster.precision',
				id : 'precision',
				store :new Ext.data.SimpleStore({
							fields : ['key', 'value'],
							data : Ext.socialType.precisionStore
						}),
				displayField : 'value',
				typeAhead : true,
				valueField : 'key',
				mode : 'local',
				value:'2',
				triggerAction : 'all',
				selectOnFocus : true,
				emptyText : '请选择...',
				readOnly : true,
				anchor : '90%'
			});

	// 个人金额精确值下拉框
	var personPrecisionCombox = new Ext.form.ComboBox({
				fieldLabel : '个人金额精确值',
				hiddenName:'pbSocialMaster.person_precision',
				name : 'pbSocialMaster.person_precision',
				id : 'person_precision',
				store :new Ext.data.SimpleStore({
							fields : ['key', 'value'],
							data : Ext.socialType.precisionStore
						}),
				displayField : 'value',
				typeAhead : true,
				valueField : 'key',
				mode : 'local',
				value:'2',
				triggerAction : 'all',
				selectOnFocus : true,
				emptyText : '请选择...',
				readOnly : true,
				anchor : '90%'
			});

	// 企业计算方法的下拉框
	var caculateTypeCombox = new Ext.form.ComboBox({
				fieldLabel : '企业计算方法',
				hiddenName:'pbSocialMaster.caculate_type',
				name : 'pbSocialMaster.caculate_type',
				id : 'caculate_type',
				store : new Ext.data.SimpleStore({
							fields : ['key', 'value'],
							data : Ext.socialType.caculateTypeStore
						}),
				displayField : 'value',
				typeAhead : true,
				valueField : 'key',
				mode : 'local',
				value:'2',
				triggerAction : 'all',
				selectOnFocus : true,
				emptyText : '请选择...',
				readOnly : true,
				anchor : '90%',
				width : 150
			});

	// 个人计算方法的下拉框
	var personCaculateTypeCombox = new Ext.form.ComboBox({
				fieldLabel : '个人计算方法',
				hiddenName:'pbSocialMaster.person_caculatetype',
				name : 'pbSocialMaster.person_caculatetype',
				id : 'person_caculatetype',
				store : new Ext.data.SimpleStore({
							fields : ['key', 'value'],
							data : Ext.socialType.caculateTypeStore
						}),
				displayField : 'value',
				typeAhead : true,
				valueField : 'key',
				mode : 'local',
				value:'2',
				triggerAction : 'all',
				selectOnFocus : true,
				emptyText : '请选择...',
				readOnly : true,
				anchor : '90%',
				width : 150
			});

	// 是否需要缴税的下拉框
	var isForTaxCombox = new Ext.form.ComboBox({
				fieldLabel : '是否需要缴税',
				hiddenName:'pbSocialMaster.taxes',
				name : 'pbSocialMaster.taxes',
				id : 'taxes',
				store : new Ext.data.SimpleStore({
							fields : ['key', 'value'],
							data : Ext.socialType.isForTaxStore
						}),
				displayField : 'value',
				typeAhead : true,
				valueField : 'key',
				mode : 'local',
				value:'0',
				triggerAction : 'all',
				selectOnFocus : true,
				emptyText : '请选择...',
				readOnly : true,
				anchor : '90%',
				width : 150
			});

	// 是否允许补缴的下拉框
	var ISARREARSCombox = new Ext.form.ComboBox({
				fieldLabel : '是否允许补缴',
				hiddenName:'pbSocialMaster.is_arrears',
				name : 'pbSocialMaster.is_arrears',
				id : 'is_arrears',
				store : new Ext.data.SimpleStore({
							fields : ['key', 'value'],
							data : Ext.socialType.ISARREARSStore
						}),
				displayField : 'value',
				typeAhead : true,
				valueField : 'key',
				emptyText : '请选择...',
				readOnly : true,
				mode : 'local',
				value:'0',
				triggerAction : 'all',
				selectOnFocus : true,
				anchor : '90%',
				width : 150
			});
	var social_security_id=new Ext.form.Hidden({
			                    name: 'pbSocialMaster.social_security_id',
			                    id:'social_security_id'
			                 })
	// 添加社保项目区域
	var pbSocialTypeInfoPanel = new Ext.form.FormPanel({
				width : 800,
				height : 270,
				id : 'pbSocialTypeInfoPanel',
				region:'north',
				layout : 'column',
				frame : true,
				buttonAlign : 'left',
				bodyStyle : 'padding:5px 5px 0',
				items : [{
							columnWidth : .5,
							layout : 'form',
							items : [social_security_id
									,{
										layout : 'form',
										items : [proviceCombox]
									}, {
										fieldLabel : '<image src="'+basePath+'images/must.gif"/>社保项目名称',
										anchor : '90%',
										name : 'pbSocialMaster.show_name',
										id : 'show_name',
										allowBlank:false,
										emptyText : '请选择...',
										blankText:'请输入社保项目名称!',
										xtype : 'textfield'
									}, {
										layout : 'form',
										items : [socialTypeCombox]
									}, {
										layout : 'form',
										items : [payAtMonthCombox]
									}, {
										layout : 'form',
										items : [personPrecisionCombox]
									}, {
										layout : 'form',
										items : [personCaculateTypeCombox]
									}, {
										layout : 'form',
										items : [ISARREARSCombox]
									}]
						}, {
							columnWidth : .5,
							layout : 'form',
							items : [{
										columnWidth : .5,
										layout : 'form',
										items : [cityCombox]
									}, {
										layout : 'form',
										items : [payFrequencyCombox]
									}, {
										layout : 'form',
										items : [isValidCombox]
									}, {
										layout : 'form',
										items : [precisionCombox]
									}, {
										layout : 'form',
										items : [caculateTypeCombox]
									}, {
										layout : 'form',
										items : [isForTaxCombox]
									}]
						}, {
							columnWidth : 1,
							layout : 'form',
							items : [{
										xtype : 'textarea',
										anchor : '95%',
										height : 70,
										fieldLabel : '说明',
										name : 'pbSocialMaster.memo',
										id : 'memo'
									}]
						}]
			});
			addOnChangedEvent(pbSocialTypeInfoPanel);

	// 社保子项表格
	var newStore = new Ext.data.Store({
				pruneModifiedRecords:true,
				proxy:new Ext.data.MemoryProxy(Ext.util.JSON.decode("[]")),
				//url : 'socialItem!queryPbSocialDetailResult.action',
				reader : new Ext.data.JsonReader({
							//root : 'pbSocialDetailResult.result',
							//id:'sub_social_security_id',
							totalProperty : 'total'
						}, [{		name:'sub_social_security_id',
									mapping : 'sub_social_security_id'
								},{		name:'social_security_id',
									mapping : 'social_security_id'
								},{
									name : 'start_month',
									mapping : 'start_month'
								}, {
									name : 'end_month',
									mapping : 'end_month'
								}, {
									name : 'person_base_min',
									mapping : 'person_base_min'
								}, {
									name : 'person_base_max',
									mapping : 'person_base_max'
								}, {
									name : 'person_base_default',
									mapping : 'person_base_default'
								}, {
									name : 'person_percent_min',
									mapping : 'person_percent_min'
								}, {
									name : 'person_percent_max',
									mapping : 'person_percent_max'
								}, {
									name : 'person_percent_default',
									mapping : 'person_percent_default'
								}, {
									name : 'company_base_min',
									mapping : 'company_base_min'
								}, {
									name : 'company_base_max',
									mapping : 'company_base_max'
								}, {
									name : 'company_base_default',
									mapping : 'company_base_default'
								}, {
									name : 'company_percent_min',
									mapping : 'company_percent_min'
								}, {
									name : 'company_percent_max',
									mapping : 'company_percent_max'
								}, {
									name : 'company_percent_default',
									mapping : 'company_percent_default'
								}, {
									name : 'company_precision',
									mapping : 'company_precision'
								}, {
									name : 'person_precision',
									mapping : 'person_precision'
								}, {
									name : 'company_add',
									mapping : 'company_add'
								}, {
									name : 'person_add',
									mapping : 'person_add'
								}, {
									name : 'tax_start',
									mapping : 'tax_start'
								}, {		
									name:'in_used',
									mapping : 'in_used',
									type:'bool'
								}])
			});

	// 查询显示的表格
	var checkColumn = new Ext.grid.CheckColumn({    
        header: '是否有效',    
        dataIndex: 'in_used',    
        width: 60    
    });  
	var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), sm, {
			header : 'ID',
				dataIndex : 'sub_social_security_id',
				hidden:true
			},{
			header : '社保ID',
				dataIndex : 'social_security_id',
				hidden:true
			},{
				header : '起始月',
				dataIndex : 'start_month',
	            editor: new Ext.form.TextField({
	            		allowBlank:false
	            })
			}, {
				header : '终止月',
				dataIndex : 'end_month',
	            editor: new Ext.form.TextField({
	            })
			}, {
				header : '个人基数下限',
				dataIndex : 'person_base_min',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			}, {
				header : '个人基数上限',
				dataIndex : 'person_base_max',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			},{
				header : '个人基数推荐值',
				dataIndex : 'person_base_default',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			}, {
				header : '个人比例下限',
				dataIndex : 'person_percent_min',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:4
	            })
			}, {
				header : '个人比例上限',
				dataIndex : 'person_percent_max',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:4
	            })
			},  {
				header : '个人比例推荐值',
				dataIndex : 'person_percent_default',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:4
	            })
			}, {
				header : '企业基数下限',
				dataIndex : 'company_base_min',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			}, {
				header : '企业基数上限',
				dataIndex : 'company_base_max',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			}, {
				header : '企业基数推荐值',
				dataIndex : 'company_base_default',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			}, {
				header : '企业比例下限',
				dataIndex : 'company_percent_min',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:4
	            })
			}, {
				header : '企业比例上限',
				dataIndex : 'company_percent_max',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:4
	            })
			}, {
				header : '企业比例推荐值',
				dataIndex : 'company_percent_default',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:4
	            })
			}, {
				header : '企业金额精确值',
				dataIndex : 'company_precision',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			}, {
				header : '个人金额精确值',
				dataIndex : 'person_precision',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			}, {
				header : '企业附加金额',
				dataIndex : 'company_add',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			}, {
				header : '个人附加金额',
				dataIndex : 'person_add',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			}, {
				header : '计税起征点',
				dataIndex : 'tax_start',
	            editor: new Ext.form.NumberField({
	            		allowBlank:false,
						allowNegative:false,
						decimalPrecision:2
	            })
			},checkColumn]);

	var item = Ext.data.Record.create([
				{name:'sub_social_security_id',type:'int'},
				{name:'social_security_id',type:'int'},
				{name:'start_month',type:'date'},
				{name:'end_month',type:'date'},
				{name:'person_base_min',type:'float'},
				{name:'person_base_max',type:'float'},
				{name:'person_percent_min',type:'float'},
				{name:'person_percent_max',type:'float'},
				{name:'person_base_default',type:'float'},
				{name:'person_percent_default',type:'float'},
				{name:'company_base_min',type:'float'},
				{name:'company_base_max',type:'float'},
				{name:'company_percent_min',type:'float'},
				{name:'company_percent_max',type:'float'},
				{name:'company_base_default',type:'float'},
				{name:'company_percent_default',type:'float'},
				{name:'company_precision',type:'float'},
				{name:'person_precision',type:'float'},
				{name:'company_add',type:'float'},
				{name:'person_add',type:'float'},
				{name:'tax_start',type:'float'},
				{name:'in_used',type:'bool'}
			  ]);
			  var addAction = new Ext.Action({
				text:'新增子项',
				//iconCls:'all',
				handler:function() {
					var initValue = {sub_social_security_id:'',
									social_security_id:pbSocialTypeInfoPanel.findById('social_security_id').getValue(),
									in_used:1,
									start_month:'',
									end_month:'',
									person_base_min:'',
									person_base_max:'',
									person_percent_min:'',
									person_percent_max:'',
									person_base_default:'',
									person_percent_default:'',
									company_base_min:'',
									company_base_max:'',
									company_percent_min:'',
									company_percent_max:'',
									company_base_default:'',
									company_percent_default:'',
									company_precision:'2',
									person_precision:'2',
									company_add:'0',
									person_add:'0',
									tax_start:'0'
									};
					var p = new item(initValue);
					//alert(Ext.encode(p));
					socialMasterInfoGrid.stopEditing();
					newStore.insert(0,p);
					socialMasterInfoGrid.startEditing(0,2);
					socialMasterInfoGrid.view.refresh();
					p.dirty=true;
					p.modified = initValue;
					if (newStore.modified.indexOf(p) == -1) {
						newStore.modified.push(p);
					}
				}
			  });
			  var delAction = new Ext.Action({
				text:'删除子项',
				handler:function(){
					var length = socialMasterInfoGrid.getSelectionModel().getSelections().length;
					if (length > 0){
						Ext.MessageBox.show({
							title:'提示',
							msg:'您选中了' + length + '条记录进行删除,请问您确认吗?',
							buttons:Ext.MessageBox.OKCANCEL,
							fn:function(btn) {
								if (btn == "ok"){
									var rs = socialMasterInfoGrid.getSelectionModel().getSelections();
									for (var i = 0;i < length ; i++){
										if (rs[i].data.sub_social_security_id != "") {
											deletedJson.push(rs[i].data);
										}
										newStore.remove(newStore.getById(rs[i].id));
									};
								}
								if (btn == "cancel"){
									return;
								}
							}
						});
					} else {
						Ext.Msg.alert('警告','请选择需要删除的记录.');
					}
				}
			  });
			  		  
	var socialMasterInfoGrid = new Ext.grid.EditorGridPanel({
				clicksToEdit:1,
				height : 210,
				anchor : '90%',
				region:'center',
				autoScroll:true,
				id : 'socialMasterInfoGrid',
				tbar:[addAction,delAction],
				bbar : new Ext.PagingToolbar({
							pageSize : pageSize,
							store : newStore,
							displayInfo : true,
							dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
							emptyMsg : '没有记录'
						}),
				sm : sm,
				cm : cm,
				plugins:checkColumn,  
				store : newStore,
				loadMask : true,
				loadMask : {
					msg : '正在加载数据...'
				}
			});
			newStore.load();
	// 编辑社保项目window
	var win = new Ext.Window({
				layout:'border',
				closeAction:'hide',
				closable : true,
				draggable : true,
				region : 'center',
				title : '编辑社保项目',
				width : 810,
				height : 550,
				plain : true,
				items : [pbSocialTypeInfoPanel, socialMasterInfoGrid],
				buttons : [{
					text:'确认',
					id : 'save',
					handler:function() {
						
						//校验表单合法性
						if (Ext.getCmp('pbSocialTypeInfoPanel').getForm().isValid()) {
							
					    //表单是否被更改的标志
						if (!changedFlag) {
							newStore.each(function(item){
								if (changedFlag || item.dirty){
									changedFlag = true;
								}
							});
							if(deletedJson!=[]){
								changedFlag = true;
							}
						}
						//表单没有被更改
						if (!changedFlag){
						    changedFlag = false;
							deletedJson = [];
							pbSocialTypeInfoPanel.getForm().reset();
							newStore.loadData(Ext.util.JSON.decode("[]"),false);
							win.hide();
							return;
						}
						
					    var modified = newStore.getRange(0,newStore.getCount()).slice(0);
					    //校验EditorGrid数据的合法性
						for (var i = 0; i < modified.length; i++) {
							var record = modified[i];
							var fields=record.fields.keys;
							//此处从2开始是避免了没有editor的两个隐藏字段
							for (var j = 2; j<fields.length-1; j++) {
								var name = fields[j];
								var value = record.data[name];
								var colIndex = cm.findColumnIndex(name);
								var rowIndex = newStore.indexOfId(record.id);
								var editor = cm.getCellEditor(colIndex).field;
								if(name=='start_month'){
									if(!checkDate(value,rowIndex,colIndex)){
										return;
									}
								}
								if (name == 'end_month' && value != null&&value != ''){
									if(!checkDate(value,rowIndex,colIndex)){
										return;
									}
									if (record.data[fields[j-1]]>value) {
										Ext.MessageBox.alert('Error','终止月必须大于起始月！',function(){
											socialMasterInfoGrid.startEditing(rowIndex,cm.findColumnIndex('end_month'));
										});
										return;
									} 
								}
								if(name=='person_base_min'){
									if(value!=null&&value!=''&&record.data[fields[j+1]]!=null&&record.data[fields[j-1]]!=''){
										if(value>record.data[fields[j+1]]){
											Ext.MessageBox.alert('Error','个人基数上限必须大于下限！',function(){
												socialMasterInfoGrid.startEditing(rowIndex,cm.findColumnIndex('person_base_max'));
											});
											return;
										}
									}
									if(record.data[fields[j+2]]!=null&&record.data[fields[j+2]]!=''){
										if(record.data[fields[j+2]]<value||record.data[fields[j+2]]>record.data[fields[j+1]]){
											Ext.MessageBox.alert('Error','个人基数推荐值必须在上限及下限之间！',function(){
												socialMasterInfoGrid.startEditing(rowIndex,cm.findColumnIndex('person_base_default'));
											});
											return;
										}
									}
								}
								if(name=='person_percent_min'){
									if(value!=null&&value!=''&&record.data[fields[j+1]]!=null&&record.data[fields[j-1]]!=''){
										if(value>record.data[fields[j+1]]){
											Ext.MessageBox.alert('Error','个人比例上限必须大于下限！',function(){
												socialMasterInfoGrid.startEditing(rowIndex,cm.findColumnIndex('person_percent_max'));
											});
											return;
										}
									}
									if(record.data[fields[j+2]]!=null&&record.data[fields[j+2]]!=''){
										if(record.data[fields[j+2]]<value||record.data[fields[j+2]]>record.data[fields[j+1]]){
											Ext.MessageBox.alert('Error','个人比例必须在上限及下限之间！',function(){
												socialMasterInfoGrid.startEditing(rowIndex,cm.findColumnIndex('person_percent_default'));
											});
											return;
										}
									}
								}
								if(name=='company_base_min'){
									if(value!=null&&value!=''&&record.data[fields[j+1]]!=null&&record.data[fields[j-1]]!=''){
										if(value>record.data[fields[j+1]]){
											Ext.MessageBox.alert('Error','公司基数上限必须大于下限！',function(){
												socialMasterInfoGrid.startEditing(rowIndex,cm.findColumnIndex('company_base_max'));
											});
											return;
										}
									}
									if(record.data[fields[j+2]]!=null&&record.data[fields[j+2]]!=''){
										if(record.data[fields[j+2]]<value||record.data[fields[j+2]]>record.data[fields[j+1]]){
											Ext.MessageBox.alert('Error','公司基数推荐值必须在上限及下限之间！',function(){
												socialMasterInfoGrid.startEditing(rowIndex,cm.findColumnIndex('company_base_default'));
											});
											return;
										}
									}
								}
								if(name=='company_percent_min'){
									if(value!=null&&value!=''&&record.data[fields[j+1]]!=null&&record.data[fields[j-1]]!=''){
										if(value>record.data[fields[j+1]]){
											Ext.MessageBox.alert('Error','公司比例上限必须大于下限！',function(){
												socialMasterInfoGrid.startEditing(rowIndex,cm.findColumnIndex('company_percent_max'));
											});
											return;
										}
									}
									if(record.data[fields[j+2]]!=null&&record.data[fields[j+2]]!=''){
										if(record.data[fields[j+2]]<value||record.data[fields[j+2]]>record.data[fields[j+1]]){
											Ext.MessageBox.alert('Error','公司比例推荐值必须在上限及下限之间！',function(){
												socialMasterInfoGrid.startEditing(rowIndex,cm.findColumnIndex('company_percent_default'));
											});
											return;
										}
									}
								}
								if (value != null && !editor.validateValue(value)){
									Ext.Msg.alert('提示','输入的数据不正确',function(){
										socialMasterInfoGrid.startEditing(rowIndex,colIndex);
									});
									return;
								}
							}
						}
						//校验EditorGrid数据的合法性
					    var nullflag=0;
						var colIndex = cm.findColumnIndex('start_month');
						for (var i = 0; i < modified.length; i++) {
							var temp = modified[i];
							var rowIndex = newStore.indexOfId(record.id);
							if(temp.data['end_month']==''||temp.data['end_month']==null){
								nullflag++;
							}
							for (var j = 0; j < modified.length; j++) {
								var record = modified[j];
								if(i!=j){
									//alert("i:"+i+";j:"+j+"temp.s:"+temp.data['start_month']+"temp.e:"+temp.data['end_month']+"record.s:"+record.data['start_month']+"record.e:"+record.data['end_month']);
									//alert(temp.data['start_month']>=record.data['end_month']);
									//alert(!(temp.data['start_month']<record.data['end_month']&&record.data['start_month']>temp.data['end_month']));
									if(temp.data['start_month']>=record.data['end_month']){
									}else if(!(temp.data['start_month']<record.data['end_month']&&record.data['start_month']>=temp.data['end_month'])){
										Ext.Msg.alert('提示','输入的数据的开始结束日期不正确！',function(){
											socialMasterInfoGrid.startEditing(rowIndex,colIndex);
										});
										return;
									}
								}
							}
						}
						if(nullflag>1){
							Ext.Msg.alert('提示','结束日期为空只能有一条记录！');
							nullflag=0;
							return;
						}else{
							nullflag=0;
						}
						var modifiedArray = [];
						Ext.each(modified,function(item){
							modifiedArray.push(item.data);
						});
							addurl='socialItem!addPbSocialMaster.action';
							updurl='socialItem!updateSocialMaster.action';
							var _url;
							var paras={};
							if(updflag!=true){
								_url=addurl;
								paras.modified=Ext.encode(modifiedArray);
							}else{
								_url=updurl;
								paras.modified=Ext.encode(modifiedArray);
								paras.deleted=Ext.encode(deletedJson);
							}
							Ext.getCmp('pbSocialTypeInfoPanel').getForm().submit({
									url:_url,
									params:paras,
									success : function(from, action) {
										Ext.Msg.alert('提示', '操作失败,失败原因!');
									},
									failure : function(form, action) {
									    changedFlag = false;
										deletedJson = [];
										pbSocialTypeInfoPanel.getForm().reset();
										newStore.removeAll();
										win.hide();socialTypeMasterStore.load();
									},
									waitMsg : '正在保存数据，稍后...'
								});
						}
						
					}
				} , {
					text : '取消',
					id : 'cancel',
					handler : function() {
						if (!changedFlag) {
							newStore.each(function(item){
								if (changedFlag || item.dirty){
									changedFlag = true;
								}
							});
						}
						if (changedFlag){
							Ext.MessageBox.show({
								title:'提示',
								msg:'表单的数据已经被修改，您确认需要放弃修改吗？',
								buttons:Ext.MessageBox.OKCANCEL,
								fn:function(btn) {
									if (btn == "ok"){
									    changedFlag = false;
										deletedJson = [];
										pbSocialTypeInfoPanel.getForm().reset();
										newStore.loadData(Ext.util.JSON.decode("[]"),false);
										Ext.getCmp('pbSocialTypeInfoPanel').getForm().reset();
										win.hide();
									}
									if (btn == "cancel"){
										return false;
									}
								}
							});	
						} else {
							Ext.getCmp('pbSocialTypeInfoPanel').getForm().reset();
							win.hide();
						}
					}
				}]
			});
			
	return win;
}
	function addOnChangedEvent(panel) {
				if (panel == null || typeof(panel) != 'object' || panel.getXType() != 'form') {
					return;	
				}
				var fields = panel.findByType('field',false);
				for (var i = 0; i < fields.length ; i++) {
					var field = fields[i];
					field.on({
						change : function(f,n,o){
							changedFlag = true;
						}
					});
				}
			}